package com.example.xunua_zhinengjiaotong.Dialog;

import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.xunua_zhinengjiaotong.R;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONObject;

import okhttp3.internal.Util;


public class ludeng1Dialog extends AlertDialog{
    private Context context;
    private Button btn_chaxun;
    private Spinner spinner;
    private TextView tv1,tv2;
    private Ludeng1Listener ludeng1Listener;  //接口
    private HttpUtil util=new HttpUtil();
    private MyApp myApp;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                tv2.setText(msg.obj.toString());
            }else if (msg.what==-1){
                Toast.makeText(context, "查询失败", Toast.LENGTH_SHORT).show();
            }
        }
    };

    public ludeng1Dialog(MyApp myApp,Context context,Ludeng1Listener ludeng1Listener) {
        super(context,R.style.myDialog);
        this.context=context;
        this.ludeng1Listener=ludeng1Listener;
        this.myApp=myApp;
    }

    protected ludeng1Dialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected ludeng1Dialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_dialog_ludeng1);
        initView();
    }

    private void initView() {
        spinner = findViewById(R.id.ludeng1_spinner);
        btn_chaxun = findViewById(R.id.ludeng1_btn_chaxun);
        tv1=findViewById(R.id.ludeng1_tv1);
        tv2=findViewById(R.id.ludeng1_tv2);
        spinner.setAdapter(new ArrayAdapter<String>(context, R.layout.support_simple_spinner_dropdown_item,
                new String[]{"1号路灯", "2号路灯", "3号路灯", "4号路灯", "5号路灯", "6号路灯", "7号路灯"}));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                tv1.setText(spinner.getItemAtPosition(position).toString()+"当前的状态：");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        btn_chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv2.setText("");
                Integer id= (int) (spinner.getSelectedItemId()+1);
                getLuDeng(id.toString());
//                ludeng1Listener.sendMessage(id.toString());
            }
        });
    }
    private void getLuDeng(final String id){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject getRoadLightStatus = util.connectServer("{\"RoadLightId\":" + id + "}", myApp, "GetRoadLightStatus");
                    String status = getRoadLightStatus.getString("Status");
                    Message message=new Message();
                    message.what=0;
                    message.obj=status;
                    handler.sendMessage(message);
                }catch (Exception e){
                    handler.sendEmptyMessage(-1);
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public interface Ludeng1Listener{
        void sendMessage(String s);
    }
}
